{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Determine if Two Strings Are Close"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: closeStrings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #确定两个字符串是否接近"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>如果可以使用以下操作从一个字符串得到另一个字符串，则认为两个字符串 <strong>接近</strong> ：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>操作 1：交换任意两个 <strong>现有</strong> 字符。\n",
    "\n",
    "\t<ul>\n",
    "\t\t<li>例如，<code>a<strong>b</strong>cd<strong>e</strong> -> a<strong>e</strong>cd<strong>b</strong></code></li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "\t<li>操作 2：将一个 <strong>现有</strong> 字符的每次出现转换为另一个 <strong>现有</strong> 字符，并对另一个字符执行相同的操作。\n",
    "\t<ul>\n",
    "\t\t<li>例如，<code><strong>aa</strong>c<strong>abb</strong> -> <strong>bb</strong>c<strong>baa</strong></code>（所有 <code>a</code> 转化为 <code>b</code> ，而所有的 <code>b</code> 转换为 <code>a</code> ）</li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "</ul>\n",
    "\n",
    "<p>你可以根据需要对任意一个字符串多次使用这两种操作。</p>\n",
    "\n",
    "<p>给你两个字符串，<code>word1</code> 和 <code>word2</code> 。如果<em> </em><code>word1</code><em> </em>和<em> </em><code>word2</code><em> </em><strong>接近 </strong>，就返回 <code>true</code> ；否则，返回<em> </em><code>false</code><em> </em>。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>word1 = \"abc\", word2 = \"bca\"\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>2 次操作从 word1 获得 word2 。\n",
    "执行操作 1：\"a<strong>bc</strong>\" -> \"a<strong>cb</strong>\"\n",
    "执行操作 1：\"<strong>a</strong>c<strong>b</strong>\" -> \"<strong>b</strong>c<strong>a</strong>\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>word1 = \"a\", word2 = \"aa\"\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>不管执行多少次操作，都无法从 word1 得到 word2 ，反之亦然。</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>word1 = \"cabbba\", word2 = \"abbccc\"\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>3 次操作从 word1 获得 word2 。\n",
    "执行操作 1：\"ca<strong>b</strong>bb<strong>a</strong>\" -> \"ca<strong>a</strong>bb<strong>b</strong>\"\n",
    "执行操作 2：<code>\"</code><strong>c</strong>aa<strong>bbb</strong>\" -> \"<strong>b</strong>aa<strong>ccc</strong>\"\n",
    "执行操作 2：\"<strong>baa</strong>ccc\" -> \"<strong>abb</strong>ccc\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>word1 = \"cabbba\", word2 = \"aabbss\"\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>不管执行多少次操作，都无法从 word1 得到 word2 ，反之亦然。</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= word1.length, word2.length <= 10<sup>5</sup></code></li>\n",
    "\t<li><code>word1</code> 和 <code>word2</code> 仅包含小写英文字母</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [determine-if-two-strings-are-close](https://leetcode.cn/problems/determine-if-two-strings-are-close/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [determine-if-two-strings-are-close](https://leetcode.cn/problems/determine-if-two-strings-are-close/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"abc\"\\n\"bca\"', '\"a\"\\n\"aa\"', '\"cabbba\"\\n\"abbccc\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, s1: str, s2: str) -> bool:\n",
    "        c1, c2 = [0] * 26, [0] * 26\n",
    "        for c in s1:\n",
    "            c1[ord(c) - ord('a')] += 1\n",
    "        for c in s2:\n",
    "            c2[ord(c) - ord('a')] += 1\n",
    "        for i in range(26):\n",
    "            if c1[i] + c2[i] == 0:\n",
    "                continue\n",
    "            if c1[i] == 0 or c2[i] == 0:\n",
    "                return False\n",
    "        c1.sort()\n",
    "        c2.sort()\n",
    "        for i in range(26):\n",
    "            if c1[i] != c2[i]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        if len(word1)!=len(word2):\n",
    "            return False\n",
    "        dic1,dic2={},{}\n",
    "        # print(word2)\n",
    "        for i in word1:\n",
    "            dic1[i]=dic1.get(i,0)+1\n",
    "        for i in word2:\n",
    "            dic2[i]=dic2.get(i,0)+1\n",
    "        if dic1.keys()!=dic2.keys():\n",
    "            return False\n",
    "        # print(dic1,dic2)\n",
    "        freq1,freq2={},{}\n",
    "        for i in dic1:\n",
    "            freq1[dic1[i]]=freq1.get(dic1[i],0)+1\n",
    "        for i in dic2:\n",
    "            freq2[dic2[i]]=freq2.get(dic2[i],0)+1\n",
    "        # print(freq1,freq2)\n",
    "        return freq1==freq2\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        if len(word1) != len(word2):\n",
    "            return False\n",
    "        from collections import defaultdict\n",
    "        dic1 = defaultdict(int)\n",
    "        dic2 = defaultdict(int)\n",
    "        for c in word1:\n",
    "            dic1[c] += 1\n",
    "        for c in word2:\n",
    "            dic2[c] += 1\n",
    "        c_set1 = set(dic1.keys())\n",
    "        c_set2 = set(dic2.keys())\n",
    "        if c_set1 != c_set2:\n",
    "            return False\n",
    "        list1 = list(dic1.values())\n",
    "        list2 = list(dic2.values())\n",
    "        if len(list1) != len(list2):\n",
    "            return False\n",
    "        import heapq\n",
    "        heapq.heapify(list1)\n",
    "        heapq.heapify(list2)\n",
    "        while list1:\n",
    "            if heapq.heappop(list1) != heapq.heappop(list2):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        if len(word1)!=len(word2):\n",
    "            return False\n",
    "        for char in word1:\n",
    "            if char not in word2:\n",
    "                return False\n",
    "        for char in word2:\n",
    "            if char not in word1:\n",
    "                return False\n",
    "        import collections\n",
    "        counter_a = collections.Counter(word1)\n",
    "        counter_b = collections.Counter(word2)\n",
    "        a_times = []\n",
    "        b_times = []\n",
    "        for char in counter_a:\n",
    "            if counter_a[char] ==counter_b[char]:\n",
    "                continue\n",
    "            else:\n",
    "                a_times.append(counter_a[char])\n",
    "                b_times.append(counter_b[char])\n",
    "        if not a_times and not b_times:\n",
    "            return True\n",
    "        if sorted(a_times)==sorted(b_times):\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        if len(word1) != len(word2):\n",
    "            return False\n",
    "        counter1, counter2 = collections.Counter(word1), collections.Counter(word2)\n",
    "        for k in counter1.keys():\n",
    "            if k not in counter2:\n",
    "                return False\n",
    "        list1, list2 = sorted(list(counter1.values())), sorted(list(counter2.values()))\n",
    "        return list1 == list2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        if len(word1)!=len(word2) or set(word1)!=set(word2):\n",
    "            return False\n",
    "        list1=[0]*26\n",
    "        list2=[0]*26\n",
    "        for i in range(len(list(word1))):\n",
    "            list1[ord(word1[i])-97]=list1[ord(word1[i])-97]+1\n",
    "        for i in range(len(list(word1))):\n",
    "            list2[ord(word2[i])-97]=list2[ord(word2[i])-97]+1\n",
    "        return (sorted(list1)==sorted(list2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        from collections import defaultdict\n",
    "        count1, count2 = defaultdict(int), defaultdict(int)\n",
    "        for c in word1:\n",
    "            count1[c] += 1\n",
    "\n",
    "        for c in word2:\n",
    "            count2[c] += 1\n",
    "        key1, key2, value1, value2 = [], [], [], []\n",
    "\n",
    "        for key in count1:\n",
    "            key1.append(key)\n",
    "            value1.append(count1[key])\n",
    "\n",
    "        for key in count2:\n",
    "            key2.append(key)\n",
    "            value2.append(count2[key])\n",
    "\n",
    "        return sorted(key1) == sorted(key2) and sorted(value1) == sorted(value2) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        n1, n2 = len(word1), len(word2)\n",
    "        # 长度不一致时不能转换\n",
    "        if n1 != n2:\n",
    "            return False\n",
    "        # 先统计word1字符串出现的次数\n",
    "        w1 = [word1.count(chr(i + 97)) for i in range(26)]\n",
    "        w2 = [0] * 26\n",
    "        # 统计word2字符串出现的次数\n",
    "        for i in range(26):\n",
    "            w2[i] = word2.count(chr(i + 97))\n",
    "            # 当w2[i]出现次数而w1[i]没出现，说明两个字符串存在不同的字母，直接返回False\n",
    "            if w2[i] and not w1[i]:\n",
    "                return False\n",
    "        # 两个字符串必须有相同的字母，而且出现次数频率也相同才能转换\n",
    "        return sorted(w1) == sorted(w2)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        if len(word1) != len(word2) or set(word1) != set(word2):\n",
    "            return False\n",
    "        \n",
    "        dict_cnt1 = collections.Counter(word1)\n",
    "        dict_cnt2 = collections.Counter(word2)\n",
    "        \n",
    "        return sorted(list(dict_cnt1.values())) == sorted(list(dict_cnt2.values()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        \n",
    "        if len(word1) != len(word2):\n",
    "            return False\n",
    "\n",
    "        ht1 = {}\n",
    "        ht2 = {}\n",
    "\n",
    "        for i in range(len(word1)):\n",
    "            if word1[i] in ht1:\n",
    "                ht1[word1[i]] = ht1[word1[i]] + 1\n",
    "            else:\n",
    "                ht1[word1[i]] = 1\n",
    "\n",
    "\n",
    "        for i in range(len(word2)):\n",
    "            if word2[i] in ht2:\n",
    "                ht2[word2[i]] = ht2[word2[i]] + 1\n",
    "            else:\n",
    "                ht2[word2[i]] = 1\n",
    "\n",
    "\n",
    "        tmp1 = list(ht1.keys())\n",
    "        tmp1.sort()\n",
    "        tmp2 = list(ht2.keys())\n",
    "        tmp2.sort()\n",
    "\n",
    "        if tmp1 != tmp2:\n",
    "            return False\n",
    "        \n",
    "        \n",
    "        t1 = []\n",
    "        t2 = []\n",
    "\n",
    "        for i in ht1:\n",
    "            t1.append(ht1[i])\n",
    "        for i in ht2:\n",
    "            t2.append(ht2[i])\n",
    "\n",
    "        t1.sort()\n",
    "        t2.sort()\n",
    "\n",
    "        for i in range(len(t1)):\n",
    "            if t1[i] != t2[i]:\n",
    "                return False\n",
    "\n",
    "        return True\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        c1 = len(word1) == len(word2)\n",
    "        c2 = set(word1) == set(word2)\n",
    "        c3 = sorted(list(Counter(word1).values())) == sorted(list(Counter(word2).values()))\n",
    "        return c1 and c2 and c3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        if len(word1) != len(word2):  # 如果两个字符串长度不等，一定不能close\n",
    "            return False\n",
    "        hash_dict1 = defaultdict(int)\n",
    "        hash_dict2 = defaultdict(int)\n",
    "        # 统计两个word中所有元素出现的次数\n",
    "        for start in range(len(word1)):\n",
    "            hash_dict1[word1[start]] += 1\n",
    "            hash_dict2[word2[start]] += 1\n",
    "        if hash_dict1.keys() != hash_dict2.keys():\n",
    "            return False\n",
    "        li1 = list(hash_dict1.values())\n",
    "        li2 = list(hash_dict2.values())\n",
    "        li1.sort()\n",
    "        li2.sort()\n",
    "        if li1 == li2:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from sortedcontainers import SortedList\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        w1=SortedList()\n",
    "        w2=SortedList()\n",
    "        word1_dict=set(word1)\n",
    "        word2_dict=set(word2)\n",
    "        for i in word1_dict:\n",
    "            if i not in word2_dict:\n",
    "                return False\n",
    "            w1.add(word1.count(i))\n",
    "        for i in word2_dict:\n",
    "            w2.add(word2.count(i))              \n",
    "        return True if w1==w2 else False\n",
    "\n",
    "\n",
    "            \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        return(sorted(Counter(word1).values())==sorted(Counter(word2).values()) and (sorted(Counter(word1).keys())==sorted(Counter(word2).keys())))\n",
    "        print(Counter(word1).keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# https://space.bilibili.com/206214\n",
    "class Solution:\n",
    "    def closeStrings(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "        cs, ct = Counter(s), Counter(t)\n",
    "        return cs.keys() == ct.keys() and Counter(cs.values()) == Counter(ct.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# https://space.bilibili.com/206214\n",
    "class Solution:\n",
    "    def closeStrings(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t):\n",
    "            return False  # 提前退出\n",
    "        cs, ct = Counter(s), Counter(t)\n",
    "        return cs.keys() == ct.keys() and Counter(cs.values()) == Counter(ct.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def SortDic(self,wdic):\n",
    "        return sorted(wdic.items(),key=lambda x:x[1])\n",
    "# # 字典排序\n",
    "# entity_dict = {'5': 1,'6': 4,'2': 5}\n",
    "# # 按照字典的值进行排序\n",
    "# entity_dict_1 = sorted(entity_dict.items(), key=lambda x: x[1])\n",
    "# # 按照字典的键进行排序\n",
    "# entity_dict_2 = sorted(entity_dict.items(), key=lambda x: x[0])\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        d1={}\n",
    "        d2={}\n",
    "        if len(word1)!=len(word2):\n",
    "            return False\n",
    "\n",
    "        for i in word1:\n",
    "            if i not in d1:\n",
    "                d1[i]=1\n",
    "            else:\n",
    "                d1[i]+=1\n",
    "            \n",
    "        for i in word2:\n",
    "            if i not in d2:\n",
    "                d2[i]=1\n",
    "            else:\n",
    "                d2[i]+=1\n",
    "\n",
    "        d1=self.SortDic(d1)\n",
    "        d2=self.SortDic(d2)\n",
    "        print(d1,d2)\n",
    "        \n",
    "        # v1=[value for value in d1.values()]\n",
    "        # v2=[value for value in d2.values()]\n",
    "        s1=set()\n",
    "        s2=set()\n",
    "\n",
    "        for i in range(len(d1)):\n",
    "            # print(v1[i],v2[i])\n",
    "            if d1[i][1]!=d2[i][1]:\n",
    "                return False\n",
    "            if d1[i][0]!=d2[i][0]: \n",
    "                s1.add(d1[i][0])\n",
    "                s2.add(d2[i][0])\n",
    "        if s1!=s2:\n",
    "            return False\n",
    "\n",
    "            \n",
    "        return True\n",
    "\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        if sorted(word1)==sorted(word2):\n",
    "            return True\n",
    "        counter1=collections.Counter(word1)\n",
    "        counter2=collections.Counter(word2)\n",
    "\n",
    "        return sorted(counter1.values())==sorted(counter2.values()) and sorted(counter1.keys())==sorted(counter2.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        if len(word1) != len(word2):\n",
    "            return False\n",
    "        if set(list(word1)) - set(list(word2)) or set(list(word2)) - set(list(word1)):\n",
    "            return False\n",
    "        s1 = defaultdict(int)\n",
    "        s2 = defaultdict(int)\n",
    "        for w in word1:\n",
    "            s1[w] += 1\n",
    "        for w in word2:\n",
    "            s2[w] += 1\n",
    "        v1 = defaultdict(int)\n",
    "        v2 = defaultdict(int)\n",
    "        for w in s1:\n",
    "            v1[s1[w]] += 1\n",
    "        for w in s2:\n",
    "            v2[s2[w]] += 1\n",
    "        for v in v1:\n",
    "            if v1[v] != v2[v]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closeStrings(self, word1: str, word2: str) -> bool:\n",
    "        def getLength(word):\n",
    "            lengths = []\n",
    "            currentLen = 0\n",
    "\n",
    "            wordSort = sorted(word)\n",
    "            currentChar = wordSort[0]\n",
    "            for i in range(len(word)):\n",
    "\n",
    "                if wordSort[i] == currentChar:\n",
    "                    currentLen += 1\n",
    "                else:\n",
    "                    lengths.append(currentLen)\n",
    "                    currentLen = 1\n",
    "                    currentChar = wordSort[i]\n",
    "            lengths.append(currentLen)\n",
    "            currentChar = wordSort[i]\n",
    "            \n",
    "            return sorted(lengths)\n",
    "            \n",
    "        # 1 若不等长，false\n",
    "        # 2 若包含的元素不同，false\n",
    "        # 3 判断每个元素的数量，若数量也match则 true\n",
    "        if len(word1) != len(word2):\n",
    "            return False\n",
    "        if set(word1) != set(word2):\n",
    "            return False\n",
    "        \n",
    "        lengths1 = getLength(word1)\n",
    "        lengths2 = getLength(word2)\n",
    "        # print(lengths2)\n",
    "\n",
    "\n",
    "\n",
    "        if lengths1 == lengths2:\n",
    "            return True\n",
    "        return False\n",
    "\n",
    "            "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
